//
// Created by francklinson on 2021/12/4 AT 10:01.
// May Saint Diana bless you!
//

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>

using namespace std;

class Solution {
public:
    int canBeTypedWords(string text, string brokenLetters) {
        int start = 0;
        int ans = 0;
        int brokenLettersMask = 0;
        for (const char &x: brokenLetters) {
            brokenLettersMask |= 1 << (x - 'a');
        }
        for (int i = 0; i < text.size(); ++i) {
            if (i == text.size() - 1 || text[i + 1] == ' ') {
                ans += check(text.substr(start, i - start + 1), brokenLettersMask);
                start = i + 2;
            }
        }
        return ans;
    }

    /**
     * word中是否包含brokenLetters里的字母
     * @param word
     * @param brokenLetters
     * @return
     */
    int check(string word, int brokenLettersMask) {
        for (const char &x: word) {
            if ((1 << (x - 'a')) & brokenLettersMask)
                return 0;
        }
        return 1;
    }
};
class Solution2 { // 题解
public:
    int canBeTypedWords(string text, string brokenLetters) {
        unordered_set<char> broken;   // 无法输入的字符集合
        for (char ch: brokenLetters){
            broken.insert(ch);
        }
        int res = 0;   // 可以完全输入的单词数目
        bool flag = true;   // 当前字符所在单词是否可被完全输入
        for (char ch: text){
            if (ch == ' '){
                // 当前字符为空格，检查上一个单词状态，更新数目并初始化 flag
                if (flag){
                    ++res;
                }
                flag = true;
            }
            else if (broken.count(ch)){
                // 当前字符不可被输入，所在单词无法被完全输入，更新 flag
                flag = false;
            }
        }
        // 判断最后一个单词状态并更新数目
        if (flag){
            ++res;
        }
        return res;
    }
};


int main() {
    Solution solution;
    cout << solution.canBeTypedWords("hello world", "ad") << endl;
    return 0;
}

